package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.Article;
import com.yunji.news.mapper.ArticleMapper;
import com.yunji.news.service.ArticleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章服务实现类
 *
 * @author yunji
 * @since 2024-11-23
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    
    @Override
    public IPage<Article> getArticlePage(Integer page, Integer size, Long categoryId, Long schoolId, 
                                         Integer isTop, Integer isHot, String keyword) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectArticlePage(pageParam, categoryId, schoolId, isTop, isHot, keyword);
    }
    
    @Override
    public List<Article> getArticlesByCategoryCode(String categoryCode, Integer limit) {
        return baseMapper.selectRecommendedByCategory(categoryCode, limit);
    }
    
    @Override
    public Map<String, List<Article>> getHomeRecommended() {
        Map<String, List<Article>> result = new HashMap<>();
        
        try {
            // 获取各类推荐文章
            result.put("news", getArticlesByCategoryCode("news", 5));
            result.put("regulations", getArticlesByCategoryCode("regulations", 5));
            result.put("admission", getArticlesByCategoryCode("admission", 5));
            result.put("exam", getArticlesByCategoryCode("exam", 5));
            result.put("results", getArticlesByCategoryCode("results", 5));
        } catch (Exception e) {
            // 如果数据库查询失败，返回空的结果集而不是抛出异常
            result.put("news", new ArrayList<>());
            result.put("regulations", new ArrayList<>());
            result.put("admission", new ArrayList<>());
            result.put("exam", new ArrayList<>());
            result.put("results", new ArrayList<>());
        }
        
        return result;
    }
    
    @Override
    public List<Article> getHotArticles(Integer limit) {
        return baseMapper.selectHotArticles(limit);
    }
    
    @Override
    public void increaseViewCount(Long id) {
        baseMapper.increaseViewCount(id);
    }
    
    @Override
    public boolean publishArticle(Long id) {
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Article::getId, id)
               .set(Article::getStatus, 1)
               .set(Article::getPublishTime, LocalDateTime.now());
        return update(wrapper);
    }
    
    @Override
    public boolean unpublishArticle(Long id) {
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Article::getId, id)
               .set(Article::getStatus, 2);
        return update(wrapper);
    }
    
    @Override
    public boolean createArticle(Article article) {
        // 如果是发布状态，设置发布时间
        if (article.getStatus() != null && article.getStatus() == 1) {
            article.setPublishTime(LocalDateTime.now());
        }
        return save(article);
    }
    
    @Override
    public boolean updateArticle(Article article) {
        // 如果状态变为发布，更新发布时间
        if (article.getStatus() != null && article.getStatus() == 1) {
            Article oldArticle = getById(article.getId());
            if (oldArticle != null && !oldArticle.getStatus().equals(1)) {
                article.setPublishTime(LocalDateTime.now());
            }
        }
        return updateById(article);
    }
    
    @Override
    public boolean deleteArticle(Long id) {
        return removeById(id);
    }
}